import { observable, action, useStrict, computed, asMap, toJS } from 'mobx';
import { Browser,BrowserHrm,BrowserCity,BrowserHrmCondition,SelectPage, Upload } from 'weaver-mobile-page';
import * as Util from '../util/formUtil';
import { _,FieldUtil } from '../util/pcPublic';

export default class FieldEntity{
    fieldMark = "";
    @observable value = "";
    @observable specialobj = null;
    @computed get numValue(){   //字段值转换为数值类型
        let _value = this.value.replace(/,/g, "");
        return FieldUtil.judgeIsNumber(_value) ? parseFloat(_value) : 0;;
    }
    @computed get valueObj(){
        return {value:this.value, specialobj:this.specialobj};
    }
    @observable variable = observable.map ? observable.map({}) : asMap({});     //字段可变区(viewattr、optionRange、customBrowserProp等)
    @observable fieldStore = null;
    fieldType = "";

    constructor(fieldMark="", valueObj={}){
        this.fieldMark = fieldMark;
        this.changeValueObj(valueObj);
    }

    @action('修改字段值对象')
    changeValueObj = (valueObj={}) =>{
        this.value = Util.convertNull(valueObj.value);
        this.specialobj = valueObj.specialobj || null;
    }

    @action('修改字段变量区')
    changeVariable = (obj={}) =>{
        !_.isEmpty(obj) && _.forIn(obj, (value,key) =>{
            this.variable.set(key, value);
        });
    }

    @action('初始化Store对象')
    initFieldStore = (fieldMark, fieldType, isCardEdit) =>{
        if(!fieldMark || fieldType.indexOf("-") < 0)
            return;
        this.fieldType = fieldType;
        const typeArr = fieldType.split("-");
        const htmltype = parseInt(typeArr[0]);
        const detailtype = parseInt(typeArr[1]);
        const isDetail = fieldMark.indexOf("_") > -1;
        const curRoute = isDetail && isCardEdit ? "/req/editDetailRow" : "/req";
        if(htmltype === 3){     //浏览框
            if(detailtype === 34 || detailtype === 2 || detailtype === 19 || detailtype === 290)       //日期时间
                return;
            const option = {id:fieldMark, route:curRoute};
            if(detailtype === 1 || detailtype === 17)   //人力资源相关
                this.fieldStore = new BrowserHrm.Store(option);
            else if(detailtype === 141)     //人力资源条件
                this.fieldStore = new BrowserHrmCondition.Store(option);
            else if(detailtype === 258 || detailtype === 263 || detailtype === 58 || detailtype === 2222)   //国家、城市、区县
                this.fieldStore = new BrowserCity.Store(option);
            else
                this.fieldStore = new Browser.Store(option);
        }else if(htmltype === 5){   //选择框
            if(detailtype === 2)
                this.fieldStore = new SelectPage.Store({id:fieldMark, route:curRoute});
        }else if(htmltype === 6){//附件上传字段
                this.fieldStoreEdit = new Upload.Store({route:curRoute, id:fieldMark+"_uploadadjustmentshow", editNameId:"resetname"});
                this.fieldStoreEdit.initLayout(curRoute );
                this.fieldStore = new Upload.Store({route:"/req", id:fieldMark+"_uploadadjustmentedit", editNameId:"resetname"});
                this.fieldStore.initLayout("/req" );
        }
    }
    
}